package com.sinoglobal.wallet.activity;

import com.sinoglobal.wallet.R;
import com.sinoglobal.wallet.api.RemoteImpl;
import com.sinoglobal.wallet.entity.W_DynamicKeyEntity;
import com.sinoglobal.wallet.entity.W_VerifyPassVo;
import com.sinoglobal.wallet.task.MyAsyncTask;
import com.sinoglobal.wallet.widget.WGridPasswordView;
import com.sinoglobal.wallet.widget.WGridPasswordView.PasswordListener;
import com.sinoglobal.wallet.widget.KeyboardUtil;
//import com.sinoglobal.wallet.widget.GridPasswordView;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.TextView;

/**
 * 
 * @author lijingjing
 * @createdate 2015-10-23 下午5:34:30
 * @Description: TODO(输入原密码)
 */
public class W_MineOldPayPasswordActivity extends W_SinoBaseActivity {
	private WGridPasswordView oldPayPassword_wordView;
	private TextView oldPayPassword_NextOk;
	private W_MineOldPayPasswordActivity context;
	private KeyboardUtil keyUtil;
	private String key_id, key_key;

	@SuppressLint("ClickableViewAccessibility") @Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		context = this;
		setContentView(R.layout.wallet_oldpay_password);
		mTemplateTitleText.setText("支付密码");
		oldPayPassword_wordView = (WGridPasswordView) findViewById(R.id.oldPayPassword_wordView);
		keyUtil = new KeyboardUtil(context, 
				oldPayPassword_wordView.getEditText(), false);
		oldPayPassword_wordView.setOnTouchListener(new OnTouchListener() {
			
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				oldPayPassword_wordView.setKeyUtil(keyUtil);
				keyUtil.showKeyboard();
				return false;
			}
		});
		oldPayPassword_wordView.setPasswordListener(new PasswordListener() {
			
			@Override
			public void setPassWord(String pwd) {
				if(pwd.length()==6){
					if(keyUtil.getShowKeyboard()){
						keyUtil.hideKeyboard();
					}
				}
			}
		});
		oldPayPassword_NextOk = (TextView) findViewById(R.id.oldPayPassword_NextOk);
		oldPayPassword_NextOk.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		int id = v.getId();
		if(id == R.id.oldPayPassword_NextOk){
			if(oldPayPassword_wordView.getPassWord().toString().equals("")){
				showToast("请输入原密码");
			}else{
				getDynamickey();
			}
		}else if(id==R.id.wallet_noData){
			loadError(false);
		}
	}
	
	//获取秘钥
	private void getDynamickey(){

		MyAsyncTask<W_DynamicKeyEntity> task = new MyAsyncTask<W_DynamicKeyEntity>(true,this,true) {

			@Override
			public W_DynamicKeyEntity before(Void... params) throws Exception {
				// TODO 测试接口
				return RemoteImpl.getInstance().getDynamicKey();
			}
			@Override
			public void after(W_DynamicKeyEntity result) {
				key_id = result.getId();
				key_key = result.getEncryptKey();
				Log.d("....", key_key + "......" + key_id);
				getOldPassWord();

			}

			@Override
			public void exception() {
			}
		};
		task.execute();
	
	
	}
	//验证支付密码接口
	private void getOldPassWord(){

		MyAsyncTask<W_VerifyPassVo> task = new MyAsyncTask<W_VerifyPassVo>(true,this,true) {

			@Override
			public W_VerifyPassVo before(Void... params) throws Exception {
				// TODO 测试接口
				return RemoteImpl.getInstance().verifyPassWord(W_MineOldPayPasswordActivity.this, 
						oldPayPassword_wordView.getPassWord().toString(), key_id, key_key);
			}
			@Override
			public void after(W_VerifyPassVo result) {

				if (result.getCode().equals("100")) {
					if(result != null){
						Bundle bundle = new Bundle();
						bundle.putString("flag", "1");
						bundle.putString("tit", "3");
						goIntent(W_PayPasswordSureOK.class, bundle);
						finish();
					}
				} else {
					showToast(result.getMsg());
				}
			
			}

			@Override
			public void exception() {
			}
		};
		task.execute();
	
	}
}
